﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Framework.LinkTrackingLog.MyTest
{
    /// <summary>
    /// 方法监控实体对象
    /// </summary>
    public class MethodMonitoringModel
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        public string Id
        {
            get
            {
                byte[] byArray = MD5.HashData(Encoding.UTF8.GetBytes($"{ClassName}.{Name}"));
                return Convert.ToHexString(byArray);
            }
        }

        /// <summary>
        /// 方法名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 类名
        /// </summary>
        public string ClassName { get; set; }
        /// <summary>
        /// 函数返回值
        /// </summary>
        public object ResponseData {  get; set; }
        /// <summary>
        /// 函数入参
        /// </summary>
        public object RequestData {  get; set; }
        /// <summary>
        /// 调用子函数
        /// </summary>
        public List<MethodMonitoringModel> ChildLevelModel { get; set; } = [];
        /// <summary>
        /// 函数执行开始时间
        /// </summary>
        public DateTime StartTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 函数执行结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        public Exception Exception { get; set; }
        /// <summary>
        /// 函数执行耗时
        /// </summary>
        public double? TotalTime 
        { 
            get 
            {
                if (EndTime == null) return null;

                return (StartTime - EndTime).Value.TotalMilliseconds;
            }
        }

        /// <summary>
        /// 函数是否执行结束
        /// </summary>
        public bool IsEnd {  get; set; } = false;
    }
}
